<template>
    <div class="gameDetailsRecordForm">
      <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
      <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
      <el-dialog title="新增比赛详情" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="gameDetailsRecord" label-width="80px" ref="addGameDetailsRecordForm">
        <el-form-item label="比赛记录" prop="crno">
          <el-col :span="6">
            <el-input v-model="gameDetailsRecord.crno" placeholder="请填入比赛记录" @input="updateView($event)" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="球员" prop="gdrplayer">
          <el-col :span="6">
            <el-input v-model="gameDetailsRecord.gdrplayer" placeholder="请填入球员" @input="updateView($event)" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="选择时间" prop="gdrtime">
          <el-col :span="4">
            <div class="block">
              <el-date-picker
                v-model="gameDetailsRecord.gdrtime"
                type="datetime"
                placeholder="选择日期时间"
                default-time="00:00:00">
              </el-date-picker>
            </div>
          </el-col>
        </el-form-item>
        <el-form-item label="比赛详情类型" prop="tocdno" label-width="100px">
          <el-col :span="12">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入详情"
              v-model="gameDetailsRecord.tocdno"
              maxlength="200"
              show-word-limit
            >
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="结果" prop="result">
          <el-col :span="12">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入详情"
              v-model="gameDetailsRecord.result"
              maxlength="200"
              show-word-limit
            >
            </el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <el-row>
         <el-col :span="12">
         <span>&nbsp;</span></el-col>
        <el-col :span="6">
           <span><el-button type="primary" @click="addGameDetailsRecord('addGameDetailsRecordForm')">增加</el-button></span>
        </el-col>
    </el-row>
      </el-dialog>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
    export default {
        name: "addGameDetailsRecord",
        data(){
          return {
            gameDetailsRecord:{crno:"",tocdno:"",gdrtime:"",gdrplayer:"",result:""},
            addDialogIsShow:false,
          }
        },
      methods: {
        init() {
          this.addDialogIsShow = true;
        },
        addGameDetailsRecord:function(addGameDetailsRecordForm){
          this.$refs[addGameDetailsRecordForm].validate(valid => {//此处refs对象和标签上的ref对应
            if (valid) {
              //如果通过验证 to do...
              console.log(this.gameDetailsRecord);
              this.setDataFromAxios();
              this.handleClose();
            } else {
              console.log('error submit!!')
              return false
            }
          });
        },
        updateView(e) {
          this.$forceUpdate();
        },
        setDataFromAxios:function(){
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios("/api/gameDetailsRecord",this.gameDetailsRecord,"post")
            .then(res=>{
              this.$message.success("增加成功")
              location.reload();
            });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        //关闭页面
        handleClose() {
          this.addDialogIsShow = false;
          //给父组件传参
          this.$emit("addDialogIsShow", this.addDialogIsShow);
        },
      },
      //监听
      watch: {
        addDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
          this.addDialogIsShow = newValue;
        }
      }
    }
</script>
<style scoped lang="stylus">
 .gameDetailsRecordForm
   margin 0 auto
   width 1000px
   text-align left

   .el-form
     text-align:center;
</style>
